#include "HHY_CLOCK_315MHZ.h"

volatile HHYLock_t HHYLock;

#ifdef LX1528
//请参考官网 http://www.chiptrue.com/product/info/4.html
void fun_315MHz_Tx_0()
{
    TX_315MHZ_IO_H();
    //Delay_us(HHY_DATAWIDTH/4);
    GCC_DELAY(696);//348us
    TX_315MHZ_IO_L();
    //Delay_us(HHY_DATAWIDTH/4*3);
    GCC_DELAY(2088);//1044us
}
void fun_315MHz_Tx_1()
{
    TX_315MHZ_IO_H();
    //Delay_us(HHY_DATAWIDTH/4*3);
    GCC_DELAY(2088);//1044us
    TX_315MHZ_IO_L();
    //Delay_us(HHY_DATAWIDTH/4);
    GCC_DELAY(696);//348us
}
void fun_315MHz_Tx_HoldCode()
{
    TX_315MHZ_IO_H();
    //Delay_us(HHY_DATAWIDTH/4);
    GCC_DELAY(696);//348us
    TX_315MHZ_IO_L();
    //Delay_us(HHY_DATAWIDTH/4*31);
    GCC_DELAY(21576);//10.788ms
}
#endif

void fun_315MHz_Tx_Data()
{
    unsigned char i,cnt;
    unsigned long password;
    unsigned char data;
  
    //同步码
    fun_315MHz_Tx_HoldCode();
    //发送4次
    for(cnt=0;cnt<4;cnt++) 
    {
        password = HHYLock.PassWord;
        //内码password
        for(i=0;i<20;i++)
        {
            if((password & 0x080000) == 0x080000)//最高位为1 ,20位
            {
                fun_315MHz_Tx_1();
            }
            else
            {
                fun_315MHz_Tx_0();
            }
            password <<= 1;
        }
        data = HHYLock.Data;
        //Data
        for(i=0;i<4;i++)
        {
            if((data & 0x08) == 0x08)//最高位为1 ,4位
            {
                fun_315MHz_Tx_1();
            }
            else
            {
                fun_315MHz_Tx_0();
            }
            data <<= 1;
        }
        //同步码
        fun_315MHz_Tx_HoldCode();      
        
    }
}


void fun_HHYLock_Key_Tx()
{
	switch(HHYLock.KeyValue)
    {
        case KEY_UNLOCK : //解锁
            HHYLock.Data = 0B00000010;
            fun_315MHz_Tx_Data();
            HHYLock.KeyValue = KEY_NONE;
            HHYLock.LEDTurn_TimeCnt = 0;
            gu16v_HaltTime = 0;
        break;
        case KEY_STARTUP : //启动
        	HHYLock.Data = 0B00000100;
        	fun_315MHz_Tx_Data();
        	HHYLock.KeyValue = KEY_STARTUP_Again;
            HHYLock.StartTime_Cnt = 0;
            HHYLock.LEDTurn_TimeCnt = 0;
            gu16v_HaltTime = 0;
        break;
        case KEY_STARTUP_Again :
        	if(HHYLock.StartTime_Cnt == (LOCK_START_AGAIN_TIME / TIMER_MS))//800ms
        	{
        		HHYLock.Data = 0B00000100;
        		fun_315MHz_Tx_Data();	
        		HHYLock.KeyValue = KEY_NONE;
        		HHYLock.LEDTurn_TimeCnt = 0;
        		gu16v_HaltTime = 0;
        	}
        break;
        case KEY_LOCK : //锁车
            HHYLock.Data = 0B00001000;
            fun_315MHz_Tx_Data();
            HHYLock.KeyValue = KEY_NONE;
            HHYLock.LEDTurn_TimeCnt = 0;
            gu16v_HaltTime = 0;
        break;
        case KEY_NONE:
        	fun_HHYClock_LED();
        break;
        default:
        break;
    }	
}

void fun_HHYClock_Init()
{
    _pdc0 = 0;
    BLE.flag.b.Tx_LowPower_On = 1;
    BLE.flag.b.Rx_LowPower_On = 0;
    
    BLE.flag.b.Tx_SysClock_Off = 1;
    BLE.flag.b.Rx_SysClock_Off = 0;
    
    HHYLock.PassWord = HHY_PASSWORD;
}      

void fun_HHYClock_LED()
{
	//低頻閃爍，高頻閃爍在發送時
	if(_pb0)
	{
		if(HHYLock.LEDTurn_TimeCnt >= 50 / TIMER_MS)	
		{
			HHYLock.LEDTurn_TimeCnt = 0;
			_pb0 = 0;
		}
	}
	else
	{
		if(HHYLock.LEDTurn_TimeCnt >= 950 / TIMER_MS)	
		{
			HHYLock.LEDTurn_TimeCnt = 0;
			_pb0 = 1;
		}	
	}
	
//	if(HHYLock.LEDTurn_TimeCnt >= 500 / TIMER_MS)	
//	{
//		 HHYLock.LEDTurn_TimeCnt = 0;
//		 TX_315MHZ_LED_IO_TURN()	
//	}
}  
        
